import 'dart:io';
import 'dart:async';
import 'package:async/async.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../provider/counter.dart';
import '../provider/cart.dart';
import './cart_page/cart_item.dart';
import 'cart_page/cart_bottom.dart';

class CartPage extends StatelessWidget {
  final AsyncMemoizer _memoizer = AsyncMemoizer();
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        debugShowCheckedModeBanner: false,
        theme: ThemeData(primaryColor: Colors.pink),
        home: Scaffold(
            appBar: AppBar(title: Text('购物车')),
            body: FutureBuilder(
              future: this._getCartInfo(context),
              builder: (context, snapshot) {
                List cartList =
                    Provider.of<CartProvider>(context, listen: false).cartList;
                switch (snapshot.connectionState) {
                  case ConnectionState.none:
                  case ConnectionState.active:
                  case ConnectionState.waiting:
                    print('cart->waiting');
                    return Center(child: CupertinoActivityIndicator());
                  case ConnectionState.done:
                    print('cart->done');
                    if (snapshot.hasData && cartList.length > 0) {
                      return Stack(
                        children: <Widget>[
                          ListView.builder(
                            itemCount: cartList.length,
                            itemBuilder: (context, index) {
                              return CartItem(item: cartList[index]);
                            },
                          ),
                          Positioned(bottom: 0, left: 0, child: CartBottom())
                        ],
                      );
                    } else {
                      return Text('购物车空空如也，去逛逛');
                    }
                }
                return null;
              },
            )));
  }

  Future<String> _getCartInfo(BuildContext context) async {
    await this._memoizer.runOnce(
        () async => await Provider.of<CartProvider>(context).getCartInfo());
    return 'done';
  }
}

class Count extends StatelessWidget {
  const Count({Key key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Text('${context.watch<Counter>().count}',
            style: Theme.of(context).textTheme.headline4));
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
        child: Scaffold(
            body: Center(
              child: Column(
                mainAxisSize: MainAxisSize.min,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  const Text('You have pushed the button this many times:'),
                  const Count()
                ],
              ),
            ),
            floatingActionButton: FloatingActionButton(
                onPressed: () => context.read<Counter>().increment(),
                tooltip: 'Increment',
                child: const Icon(Icons.add))));
  }
}

class MyButton extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: RaisedButton(onPressed: () {}, child: Text('increase')));
  }
}

class StackLayout extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Stack(
      children: <Widget>[
        new CircleAvatar(
          backgroundImage: new NetworkImage(
              'http://jspang.com/static//myimg/blogtouxiang.jpg'),
          radius: 100.0,
        ),
        new Positioned(
          top: 10.0,
          left: 10.0,
          child: new Text('JSPang.com'),
        ),
        new Positioned(
          bottom: 10.0,
          right: 10.0,
          child: new Text('技术胖'),
        )
      ],
    );
  }
}
